/*
        Firesoft 
        
    Copyright (C) 2010, Arttu Viljakainen 
    Published under GNU General Public License V3 
    (see http://www.gnu.org/licenses/licenses.html)
    
    This file is part of Firesoft.

    Firesoft is free software: 
    you can redistribute it and/or modify it under the terms of the GNU General 
    Public License as published by the Free Software Foundation, 
    either version 3 of the License, or (at your option) any later version.

    Firesoft is distributed in the hope that it will be useful, 
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 
    Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tool for Path Finding Algorithm Testing. If not, see         
    <http://www.gnu.org/licenses/>.
 */
package softstarter.filehandling.xml;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

import softstarter.launching.RunnableItem;



public class XMLDocumentWriter {
	
	private XMLStreamWriter streamWriter;
	
	public XMLDocumentWriter() {

	}
	
	public boolean writeFileObjectsToXML(String documentName, List<RunnableItem> runnableItems) {
		
		streamWriter = null;
		
		try {
			
			streamWriter = getOutputStream(documentName);
			writeDocument(runnableItems);
			
		} catch(XMLStreamException xmlse) {
			xmlse.printStackTrace();
		} catch(IOException ioe) {
			ioe.printStackTrace();
		} finally {
			if(streamWriter != null) {
				try {
					streamWriter.close();
				} catch(XMLStreamException xmlse) {
					xmlse.printStackTrace();
				}
			}
		}
		return true;
	}
	
	private XMLStreamWriter getOutputStream(String documentName) throws XMLStreamException, IOException {
        XMLOutputFactory xof = XMLOutputFactory.newInstance();
        XMLStreamWriter streamWriter = null;

        streamWriter = xof.createXMLStreamWriter(new FileOutputStream(documentName), "UTF-8");
        
        return streamWriter;
	}
	
	private void writeDocument(List<RunnableItem> runnableItems) throws XMLStreamException {
		startDocument();
		writeObjects(runnableItems);
		endDocument();
	}
	
	private void startDocument() throws XMLStreamException {
		streamWriter.writeStartDocument("UTF-8", "1.0");
		streamWriter.writeStartElement("files");
		streamWriter.writeComment("Files and folders indexed.");
	}
	
	private void endDocument() throws XMLStreamException {
		streamWriter.writeEndElement();
		streamWriter.writeEndDocument();
	}
	
	private void writeObjects(List<RunnableItem> runnableItems) throws XMLStreamException {
		for(RunnableItem file : runnableItems) {
			streamWriter.writeStartElement(FileListXMLTags.FILE.getTag());
				streamWriter.writeStartElement(FileListXMLTags.NAME.getTag());
					streamWriter.writeCharacters(file.getName());
				streamWriter.writeEndElement();
				streamWriter.writeStartElement(FileListXMLTags.PATH.getTag());
					streamWriter.writeCharacters(file.getFullPath());
				streamWriter.writeEndElement();
			streamWriter.writeEndElement();
		}
	}
	
	public static void main(String[] args) {
//		String documentName = "fileobjects.xml";
//		List<RunnableItem> fileObjects = new Indexer().getFileObjectListOfFilesIn("D:\\testifolder");
//		
//		new XMLDocumentWriter().writeFileObjectsToXML(documentName, fileObjects);
	}
}
